import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/auth';
import prisma from '@/lib/db';

export async function PATCH(request: NextRequest) {
  try {
    const session = await auth();
    
    // 检查用户是否已登录
    if (!session?.user) {
      return NextResponse.json(
        { error: '未授权访问' },
        { status: 401 }
      );
    }

    const userId = session.user.id;
    const data = await request.json();
    
    // 验证数据
    const { theme, themeColor } = data;
    
    if (!theme || !themeColor) {
      return NextResponse.json(
        { error: '缺少必要参数' },
        { status: 400 }
      );
    }

    // 验证主题值
    const validThemes = ['light', 'dark', 'system'];
    if (!validThemes.includes(theme)) {
      return NextResponse.json(
        { error: '无效的主题值' },
        { status: 400 }
      );
    }

    // 验证主题颜色值
    const validColors = ['blue', 'green', 'red', 'purple', 'orange', 'pink'];
    if (!validColors.includes(themeColor)) {
      return NextResponse.json(
        { error: '无效的主题颜色值' },
        { status: 400 }
      );
    }

    // 更新用户主题设置
    const updatedSettings = await prisma.userSettings.upsert({
      where: { userId },
      update: {
        theme,
        themeColor
      },
      create: {
        userId,
        theme,
        themeColor,
        language: 'zh_CN',
        emailNotifications: true,
        pushNotifications: true,
        weeklyDigest: true,
      }
    });

    return NextResponse.json({
      success: true,
      data: updatedSettings,
      message: '主题设置已更新'
    });
  } catch (error) {
    console.error('更新主题设置失败:', error);
    return NextResponse.json(
      { error: '更新主题设置失败' },
      { status: 500 }
    );
  }
}

export async function PUT(request: NextRequest) {
  try {
    const session = await auth();
    
    // 检查用户是否已登录
    if (!session?.user) {
      return NextResponse.json(
        { error: '未授权访问' },
        { status: 401 }
      );
    }

    const userId = session.user.id;
    const data = await request.json();
    
    // 验证数据
    const { theme, themeColor } = data;
    
    if (!theme || !themeColor) {
      return NextResponse.json(
        { error: '缺少必要参数' },
        { status: 400 }
      );
    }

    // 验证主题值
    const validThemes = ['light', 'dark', 'system'];
    if (!validThemes.includes(theme)) {
      return NextResponse.json(
        { error: '无效的主题值' },
        { status: 400 }
      );
    }

    // 验证主题颜色值
    const validColors = ['blue', 'green', 'red', 'purple', 'orange', 'pink'];
    if (!validColors.includes(themeColor)) {
      return NextResponse.json(
        { error: '无效的主题颜色值' },
        { status: 400 }
      );
    }

    // 更新用户主题设置
    const updatedSettings = await prisma.userSettings.upsert({
      where: { userId },
      update: {
        theme,
        themeColor
      },
      create: {
        userId,
        theme,
        themeColor,
        language: 'zh_CN',
        emailNotifications: true,
        pushNotifications: true,
        weeklyDigest: true,
      }
    });

    return NextResponse.json({
      success: true,
      data: updatedSettings,
      message: '主题设置已更新'
    });
  } catch (error) {
    console.error('更新主题设置失败:', error);
    return NextResponse.json(
      { error: '更新主题设置失败' },
      { status: 500 }
    );
  }
} 